package com.ruoyi.web.controller.shop;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.shop.domain.UserGoodsSpecial;
import com.ruoyi.shop.service.IUserGoodsSpecialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户商品特价Controller
 * 
 * @author ruoyi
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/shop/special")
public class UserGoodsSpecialController extends BaseController
{
    @Autowired
    private IUserGoodsSpecialService userGoodsSpecialService;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询用户商品特价列表
     */
    @PreAuthorize("@ss.hasPermi('shop:special:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserGoodsSpecial userGoodsSpecial)
    {
        startPage();
        List<UserGoodsSpecial> list = userGoodsSpecialService.selectUserGoodsSpecialList(userGoodsSpecial);
        list.forEach(item -> {
            item.setUserName(userService.selectUserById(item.getUserId()).getUserName());
        });
        return getDataTable(list);
    }

    /**
     * 导出用户商品特价列表
     */
    @PreAuthorize("@ss.hasPermi('shop:special:export')")
    @Log(title = "用户商品特价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserGoodsSpecial userGoodsSpecial)
    {
        List<UserGoodsSpecial> list = userGoodsSpecialService.selectUserGoodsSpecialList(userGoodsSpecial);
        ExcelUtil<UserGoodsSpecial> util = new ExcelUtil<UserGoodsSpecial>(UserGoodsSpecial.class);
        util.exportExcel(response, list, "用户商品特价数据");
    }

    /**
     * 获取用户商品特价详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:special:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userGoodsSpecialService.selectUserGoodsSpecialById(id));
    }

    /**
     * 新增用户商品特价
     */
    @PreAuthorize("@ss.hasPermi('shop:special:add')")
    @Log(title = "用户商品特价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserGoodsSpecial userGoodsSpecial)
    {
        UserGoodsSpecial temp = new UserGoodsSpecial();
        temp.setUserId(userGoodsSpecial.getUserId());
        temp.setGoodsId(userGoodsSpecial.getGoodsId());
        if (!userGoodsSpecialService.selectUserGoodsSpecialList(temp).isEmpty())
        {
            return AjaxResult.error("该商品已被设置特价");
        }
        return toAjax(userGoodsSpecialService.insertUserGoodsSpecial(userGoodsSpecial));
    }

    /**
     * 修改用户商品特价
     */
    @PreAuthorize("@ss.hasPermi('shop:special:edit')")
    @Log(title = "用户商品特价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserGoodsSpecial userGoodsSpecial)
    {
        return toAjax(userGoodsSpecialService.updateUserGoodsSpecial(userGoodsSpecial));
    }

    /**
     * 删除用户商品特价
     */
    @PreAuthorize("@ss.hasPermi('shop:special:remove')")
    @Log(title = "用户商品特价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userGoodsSpecialService.deleteUserGoodsSpecialByIds(ids));
    }
}
